home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 10.8 KB | 408 lines |
- package symantec.itools.awt;
-
-
- import java.awt.AWTException;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.image.FilteredImageSource;
- import java.awt.image.RGBImageFilter;
- import java.net.URL;
-
- // 01/18/97 RKM Changed setImageURL to handle getImage returning null, added call to invalidate
- // 01/29/97 TWB Integrated changes from Macintosh
-
- /* *
- * The ImageButton component is similar to a regular button except that it
- * displays an image on the button's face. The image to use is specified with
- * a URL.
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- /**
- * <b>Description</b>
- * <p>
- * This component creates a rectangular button, with an image label, that
- * initiates an action. ImageButton extends the Button class.
- * <p>
- * Use an ImageButton to:
- * <UL>
- * <DT>╖ display an image instead of text on a button.</DT>
- * <DT>╖ generate a train of action events when the user clicks the button.</DT>
- * </UL>
- * <b>Properties</b>
- * <p>
- * To select and display an image on the button, use the URL property. Specify
- * a file name for the JPEG or GIF image you want displayed. Then to scale the
- * image to fit in the button, set the Scale Mode property.
- * <p>
- * To send a train of action events when the button is clicked, set the Notify
- * While Pressed property to ôtrueö and specify a Notify Delay property value
- * in milliseconds.
- * <p>
- * To create an interaction with another component, use the Interaction Wizard.
- * <p>
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- // 01/18/97 RKM Changed setImageURL to handle getImage returning null, added call to invalidate
-
- public class ImageButton
- extends ButtonBase
- {
- /**
- * Scale image to fit button.
- */
- protected boolean scale;
- private URL url;
- private Image enabledImage;
- private Image disabledImage;
- private boolean centerMode;
-
- /**
- * Construct a new default ImageButton. Image scaling off, center mode on.
- */
- public ImageButton()
- {
- scale = false;
- enabledImage = null;
- disabledImage = null;
- url = null;
- centerMode = true;
- }
-
- /**
- * Sets the URL of the image to display on the button.
- * @param u URL of image to display
- * @see #getImageURL
- */
- public void setImageURL(URL u)
- // throws AWTException
- {
- // Remove old images
-
- enabledImage = null;
- if (disabledImage != null)
- disabledImage.flush();
- disabledImage = null;
-
- // Load new image
-
- url = u;
- Image image = getToolkit().getImage(url);
- if (image != null)
- {
- MediaTracker mt = new MediaTracker(this);
- if (mt != null)
- {
- try
- {
- mt.addImage(image, 0);
- mt.waitForAll();
- }
- catch (InterruptedException ie)
- {
- }
-
- if (mt.isErrorAny())
- {
- System.err.println("Error loading image " + image.toString());
- return;
- }
-
- enabledImage = image;
- disabledImage = createImage(
- new FilteredImageSource(image.getSource(),
- new ImageButtonDisableFilter()));
-
- //resize(image.getWidth(this) + bevel * 3 + 2, image.getHeight(this) + bevel * 3 + 2);
- }
- }
-
- invalidate();
- }
-
- /**
- * Returns the URL of the image being displayed on the button.
- * @see #setImageURL
- */
- public URL getImageURL()
- {
- return url;
- }
-
- /**
- * Sets the scale mode of the button image.
- * @param flag if true, the image is scaled to fit the button. if false,
- * the image is not scaled.
- * @see #getScaleMode
- */
- public void setScaleMode(boolean flag)
- {
- if (scale != flag)
- {
- scale = flag;
- invalidate();
- }
- }
-
- /**
- * Returns the current scale mode of the button image.
- * @see #setScaleMode
- */
- public boolean getScaleMode()
- {
- return scale;
- }
-
- /**
- * Sets the centering mode of the button image.
- * @param flag if true, the image is centered on the button. if false,
- * the image is displayed starting in the upper-left corner.
- * @see #getCenterMode
- */
- public void setCenterMode(boolean flag)
- {
- if (centerMode != flag)
- {
- centerMode = flag;
- invalidate();
- }
- }
-
- /**
- * Returns the current centering mode of the button image.
- * @see #setCenterMode
- */
- public boolean getCenterMode()
- {
- return centerMode;
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see #update
- */
- public void paint(Graphics g)
- {
- Image img = isEnabled() ? enabledImage : disabledImage;
-
- if (img == null)
- {
- super.paint(g);
- return;
- }
-
- paintHelper(g, img);
-
- Dimension s;
- int x;
- int y;
- int w;
- int h;
-
- s = size();
- x = bevel + 1 + pressedAdjustment;
- y = bevel + 1 + pressedAdjustment;
- w = s.width - 1;
- h = s.height - 1;
-
- if (symantec.itools.lang.OS.isMacintosh())
- {
- if (scale)
- {
- x = bevel + pressedAdjustment;
- y = bevel + pressedAdjustment;
- g.drawImage(img, x, y, w - bevel - bevel, h - bevel - bevel, this);
- }
- else
- {
- if(centerMode)
- {
- x += (w - bevel - bevel - img.getWidth(this)) >> 1;
- y += (h - bevel - bevel - img.getHeight(this)) >> 1;
- }
-
- g.drawImage(img, x, y, this);
- }
- }
- else
- {
- if (centerMode && !scale)
- {
- x += (w - img.getWidth(this)) / 2;
- y += (h - img.getHeight(this)) / 2;
- }
-
- if(scale)
- {
- g.drawImage(img, x, y, w - bevel * 2, h - bevel * 2, this);
- }
- else
- {
- g.drawImage(img, x, y, this);
- }
- }
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * This is a standard Java AWT method which gets called by the Java
- * AWT (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its <0,0> coordinate is this component's
- * top-left corner.
- * Typically this method paints the background color to clear the
- * component's drawing space, sets graphics context to be the foreground
- * color, and then calls paint() to draw the component.
- *
- * It is overridden here to prevent the flicker associated with the standard
- * update() method's repainting of the background before painting the component
- * itself.
- *
- * @param g the graphics context
- * @see java.awt.Component#repaint
- * @see #paint
- */
- public void update(Graphics g)
- {
- paint(g);
- }
-
- private void paintHelper(Graphics g, Image img)
- {
- Dimension s;
- int width;
- int height;
- int imgx;
- int imgy;
- int imgw;
- int imgh;
- int x;
- int y;
- int w;
- int h;
- int i;
-
- s = size();
- width = s.width;
- height = s.height;
- x = bevel + 1;
- y = bevel + 1;
- w = width - 1;
- h = height - 1;
- imgx = bevel + 1;
- imgy = bevel + 1;
- imgw = img.getWidth(this);
- imgh = img.getHeight(this);
-
- g.setColor(Color.lightGray);
-
- if (centerMode && !scale)
- {
- imgx += (width - 1 - bevel - bevel - imgw) >> 1;
- imgy += (height - 1 - bevel - bevel - imgh) >> 1;
- }
-
- if (pressed)
- {
- if(!scale)
- {
- //Clean up artifacts
- g.fillRect(0, 0, imgx + bevel, height);
- g.fillRect(imgx + imgw + bevel, 0, width - imgx + imgw + bevel, height);
- g.fillRect(imgx + bevel, 0, imgw, imgy + bevel);
- g.fillRect(imgx + bevel, imgy + imgh + bevel, imgw, height - imgy + imgh + bevel);
- //End clean up
- }
- y = x += bevel > 0 ? 2 : 1;
- g.setColor(Color.lightGray);
-
- for(i = 1; i < bevel + 1; i++)
- {
- g.drawLine(i, h - i, w - i, h - i);
- g.drawLine(w - i, h - i, w - i, i);
- }
-
- g.setColor(Color.gray);
-
- for(i = 1; i < bevel + 1; ++i)
- {
- g.drawLine(i, h, i, i);
- g.drawLine(i, i, w, i);
- }
- }
- else
- {
- if(!scale)
- {
- //Clean up artifacts
- g.fillRect(0, 0, imgx, height);
- g.fillRect(imgx + imgw, 0, width - imgx + imgw, height);
- g.fillRect(imgx, 0, imgw, imgy);
- g.fillRect(imgx, imgy + imgh, imgw, height - imgy + imgh);
- //End clean up
- }
-
- g.setColor(Color.white);
-
- for(i = 1; i < bevel + 1; i++)
- {
- g.drawLine(i, h - i, i, i);
- g.drawLine(i, i, w - i, i);
- }
-
- g.setColor(Color.gray);
-
- for(i = 1; i < bevel + 2; ++i)
- {
- g.drawLine(i, h - i, w - i, h - i);
- g.drawLine(w - i, h - i, w - i, i);
- }
- }
-
- g.setColor(Color.black);
- g.drawLine(1, 0, width - 2, 0);
- g.drawLine(0, 1, 0, height - 2);
- g.drawLine(1, height - 1, width - 2, height - 1);
- g.drawLine(width - 1, height - 2, width - 1, 1);
-
- if(showInfoTip)
- {
- if (doInfoTip)
- {
- drawInfoTip();
- }
- }
- }
- }
-
- class ImageButtonDisableFilter
- extends RGBImageFilter
- {
- public ImageButtonDisableFilter()
- {
- canFilterIndexColorModel = true;
- }
-
-
- public int filterRGB(int x, int y, int rgb)
- {
- return (rgb & ~0xff000000) | 0x80000000;
- }
- }
-
-